#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3
import hashlib

# Connect to database
conn = sqlite3.connect('db.sqlite3')
cursor = conn.cursor()

print("=" * 60)
print("Checking Admin Account")
print("=" * 60)

# Find admin user
cursor.execute("SELECT id, username, phone, password, is_admin FROM Server_user WHERE phone='13800138000'")
result = cursor.fetchone()

if result:
    user_id, username, phone, password, is_admin = result
    print("\nFound admin account:")
    print(f"  ID: {user_id}")
    print(f"  Username: {username}")
    print(f"  Phone: {phone}")
    print(f"  Is Admin: {is_admin}")
    print(f"  Password (hash): {password[:30]}...")
    
    # Django password format: algorithm$salt$hash
    # For Django's default PBKDF2
    print("\nResetting password to 'admin123'...")
    
    # Use Django's password format
    from django.contrib.auth.hashers import make_password
    new_password = make_password('admin123')
    
    cursor.execute(
        "UPDATE Server_user SET password=?, is_admin=1 WHERE phone='13800138000'",
        (new_password,)
    )
    conn.commit()
    print("SUCCESS!")
    
else:
    print("\nAdmin account NOT found!")
    print("Creating admin account...")
    
    from django.contrib.auth.hashers import make_password
    new_password = make_password('admin123')
    
    cursor.execute(
        "INSERT INTO Server_user (id, username, phone, password, is_admin, balance, head_portrait) VALUES (?, ?, ?, ?, ?, ?, ?)",
        ('admin001', 'Admin', '13800138000', new_password, 1, 0.0, '')
    )
    conn.commit()
    print("SUCCESS!")

print("\n" + "=" * 60)
print("Login Info:")
print("  Phone: 13800138000")
print("  Password: admin123")
print("=" * 60)

conn.close()
input("\nPress Enter to exit...")

